package com.clstu.draw.tankgame01;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

public class MyPanel extends JPanel implements KeyListener {//用画板来画坦克，镶嵌到画框里面才可以显示
    Hero hero = null;
    Vector<EnemyTank> enemyTanks=new Vector();//泛型推荐 前写后不写
                                  //用vector容器才没有线程安全问题。
    public MyPanel(){
        hero=new Hero(100,100);//初始化自己的坦克
        enemyTanks.add(new EnemyTank(100,500));
        enemyTanks.add(new EnemyTank(200,500));
        enemyTanks.add(new EnemyTank(300,500));
    }


    @Override
    public void paint(Graphics g) {
        super.paint(g);//初始化画笔g
        g.fillRect(0,0,1000,750);
        drawTank(hero.getX(),hero.getY(),g,hero.getDirect(),0);
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank e=enemyTanks.get(i);
            drawTank(e.getX(),e.getY(),g,e.getDirect(),1);
        }
    }

    /**
     *
     * @param x 横坐标
     * @param y 纵坐标
     * @param g 画笔
     * @param direct 方向
     * @param type 类型
     */
    private void drawTank(int x,int y,Graphics g,int direct,int type){//根据参数画出坦克，通过被paint调用画出
        switch (type){
            case 0:
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }
        switch (direct){//方向（0上1左2下3右）
            case 0://上
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x+30,y,10,60,false);
                g.fill3DRect(x+10,y+10,20,40,false);
                g.drawOval(x+10,y+20,20,20);
                g.drawLine(x+20,y,x+20,y+30);
                break;
            case 1://左
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y+30,60,10,false);
                g.fill3DRect(x+10,y+10,40,20,false);
                g.drawOval(x+20,y+10,20,20);
                g.drawLine(x,y+20,x+30,y+20);
                break;
            case 2://下
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x+30,y,10,60,false);
                g.fill3DRect(x+10,y+10,20,40,false);
                g.drawOval(x+10,y+20,20,20);
                g.drawLine(x+20,y+60,x+20,y+30);
                break;
            case 3://右
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y+30,60,10,false);
                g.fill3DRect(x+10,y+10,40,20,false);
                g.drawOval(x+20,y+10,20,20);
                g.drawLine(x+60,y+20,x+30,y+20);
                break;
            default:
                System.out.println("还没有设计出来~~");
                break;
        }
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
          if(e.getKeyCode()==KeyEvent.VK_W){
              hero.setDirect(0);
              hero.moveUP();
          }else if(e.getKeyCode()==KeyEvent.VK_A){
              hero.setDirect(1);
              hero.moveLeft();
          }else if(e.getKeyCode()==KeyEvent.VK_S){
              hero.setDirect(2);
              hero.moveDown();
          }else if(e.getKeyCode()==KeyEvent.VK_D){
              hero.setDirect(3);
              hero.moveRight();
          }
          this.repaint();//重新绘制更新后的画面
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
